using System;
using System.Collections.Generic;
using System.Linq;
using WatiN.Core;
using WatiN.Core.Constraints;
using YatFram.Browser.CssSelector;

namespace YatFram.Browser.Watin
{
    public class WatinContainerElement : WatinHtmlElement, IContainerElement
    {
        public WatinContainerElement(Element element) : base(element)
        {
        }

        public bool Exists
        {
            get { return element.Exists; }
        }

//        public IContainerElement Find(string cssSelector)
//        {
//            return new WatinContainerElement(element.FindElement(cssSelector));
//        }

        public IEnumerable<IContainerElement> Find(CssSelectors selector)
        {
            return element.DomContainer.ElementsOfType<Element>().Filter(selector.ToConstraint()).Select(e => (IContainerElement)new WatinContainerElement(e));
        }

        public InputElement FindTextBox(string cssSelector)
        {
            return new WatinTextBox(element.DomContainer.TextField(ConstraintFor(cssSelector)));
        }

        public ISingleOption FindSelect(string cssSelector)
        {
            return new WatinSelectBox(element.DomContainer.SelectList(ConstraintFor(cssSelector)));
        }

        public ISingleOption FindRadio(string name)
        {
            return new WatinRadioBox(element.DomContainer.RadioButtons.Where(button => button.GetAttributeValue("name") == name));
        }

        public IMultipleOptions FindCheckbox(string name)
        {
            return new WatinCheckBox(element.DomContainer.CheckBoxes.Where(box => box.GetAttributeValue("name") == name));
        }

        private Constraint ConstraintFor(string cssSelector)
        {
            return cssSelector.ToCssSelectors().Select(selector => selector.ToConstraint()).First();
        }
//        public IEnumerable<InputElement> FindTextBox(string cssSelector)
//        {
//            var constraint = cssSelector.ToCssSelectors().Select(selector => selector.ToConstraint()).First();
//            return new List<InputElement> { new WatinTextBox(element.DomContainer.TextField(constraint)) };
////        }
//
//        public IEnumerable<IHtmlElement> Find(string cssSelector)
//        {
//            return new List<IHtmlElement> { new WatinContainerElement(element.FindElement(cssSelector)) };
//        }
    }
}